# -*- coding: UTF-8 -*-

# @Time : 2020/8/14 16:04 

# @Author : litao

# @File : RequestUtil.py 
import requests

from .LoggerUtil import logger
from conf import config as conf

import smtplib
from email.mime.text import MIMEText
from email.header import Header
from aiohttp import ClientSession
connect_timeout, read_timeout = 1, 90.0
class RequestUtil:
    @staticmethod
    def requestpost(url, argv):
        try:
            response = requests.post(url, data=argv, timeout=(connect_timeout, read_timeout))
        except requests.exceptions.RequestException as exc:
            print('-url {0} gave error: {1!r}'.format(url, exc))
            return False
        return response

    @staticmethod
    async def asyncrequest(url, argv):
        async with ClientSession() as session:
            async with session.post(url, data=argv) as response:
                if response.status == 200:
                    responsetext = await response.read()
                else:
                    responsetext = False
                # # print(responsetext.decode('utf8'))
                # return responsetext
        return responsetext
    @staticmethod
    def sendemail():
        try:
            logger.warning(conf.NOWURL + '工厂数据Worker服务已崩，尽快修复！！！')
            message = MIMEText(conf.NOWURL + '工厂数据Worker服务已崩,尽快修复!!!', 'plain', 'utf-8')
            message['From'] = '公司服务器警告！！！@qq.com'  # Header("Server Notice", 'utf-8')     # 发送者
            message['To'] = '15179462795@163.com <15179462795@163.com>'  # 接收者

            subject = conf.NOWURL + '服务已崩，尽快修复!!!@15179462795@163.com'
            message['Subject'] = Header(subject, 'utf-8')
            smtpObj = smtplib.SMTP(conf.mail_host)
            smtpObj.connect(conf.mail_host, 25)  # 25 为 SMTP 端口号
            smtpObj.login(conf.mail_user, conf.mail_pass)
            smtpObj.sendmail(conf.sender, conf.receivers, message.as_string())
            smtpObj.sendmail(conf.sender, conf.receivers, message.as_string())
            smtpObj.sendmail(conf.sender, conf.receivers, message.as_string())
            logger.error("邮件发送成功")
        except smtplib.SMTPException:
            logger.error("Error: 无法发送邮件")